package com.jflm.service.impl;

import java.sql.Timestamp;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.jflm.common.Contants;
import com.jflm.domain.WechatTemplateMsg;
import com.jflm.domain.WxTemplateMsgData;
import com.jflm.service.IPropertyService;
import com.jflm.service.IWechatTemplateService;
import com.jflm.util.DateUtil;
import com.jflm.util.HttpUtils;
import com.jflm.util.RedisPool;

/**
 * ******************  类说明  *********************
 * class       :  WechatTemplateServiceImpl
 * @author     :  hejinyun@umfintech.com
 * @version    :  1.0  
 * description :  微信模板消息推送接口实现类
 * @see        :                        
 * ***********************************************
 */
@Service("wechatTemplateService")
public class WechatTemplateServiceImpl implements IWechatTemplateService {

	private static final Logger LOG = LoggerFactory.getLogger(WechatTemplateServiceImpl.class);
	
	@Autowired
	private IPropertyService propertyService;
	@Autowired
	private Gson gson;
	
	@Async
	public void redpakcetTemplatePush(String openid, String mobileid, Timestamp timestamp, 
			String reason, String points, String currentPoints) {
		
		String templateid = propertyService.getStrValue("wechat.template.id", "");
		String firstData = propertyService.getStrValue(templateid+".first", "");
		String color = propertyService.getStrValue(templateid+".color", "#173177");
		String remarkData = propertyService.getStrValue(templateid+".remark", "");
		
		
		Map<String, WxTemplateMsgData> msgDataMap = new HashMap<String, WxTemplateMsgData>();		
		firstData = MessageFormat.format(firstData, mobileid);
		// 微信红包鼓励金提送消息
		String ctime = DateUtil.getDateYearMonthDay(timestamp);
		WxTemplateMsgData firstMsgData = new WxTemplateMsgData(firstData, color);
		WxTemplateMsgData keyword1Data = new WxTemplateMsgData(ctime, color);
		WxTemplateMsgData keyword2Data = new WxTemplateMsgData(points, color);
		WxTemplateMsgData keyword3Data = new WxTemplateMsgData(reason, color);
		WxTemplateMsgData keyword4Data = new WxTemplateMsgData(currentPoints, color);
		WxTemplateMsgData remarkMsgData = new WxTemplateMsgData(remarkData, color);
		
		// 设置各参数的值
		msgDataMap.put("first", firstMsgData);
		msgDataMap.put("keyword1", keyword1Data);
		msgDataMap.put("keyword2", keyword2Data);
		msgDataMap.put("keyword3", keyword3Data);
		msgDataMap.put("keyword4", keyword4Data);
		msgDataMap.put("remark", remarkMsgData);
		
		WechatTemplateMsg wxTemplateMsg = new WechatTemplateMsg();
		wxTemplateMsg.setTemplate_id(templateid);
		wxTemplateMsg.setTouser(openid);
		wxTemplateMsg.setData(msgDataMap);
		
		// 获取模板消息推送url
		String access_token = RedisPool.get(Contants.ACCESS_TOKEN);
		String url = MessageFormat.format(propertyService.getStrValue("wechat.template.url", ""), access_token);
		
		// 模板消息推送
		String reqJson = gson.toJson(wxTemplateMsg);
		LOG.info("#redpakcetTemplatePush() 模板消息下发请求:{},URL:{}", reqJson, url);
		String[][] headers = {{"Content-Type", "application/json;charset:utf-8"}};
		String respJson = HttpUtils.doPost(url, reqJson, headers, 5, 8, Contants.CHARSET_UTF_8);
		LOG.info("#redpacketTemplatePush() 模板消息下发响应:{}", respJson);
	}

}
